﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Utilities
{
    public static class ExtensionsList
    {
        public static void AddValueToAllAtTheStart(this List<string> list, string value) {
            for (int i = 0; i < list.Count; i++)
            {
                list[i] = value + list[i];
            }
        }
        public static void AddValueToAllAtTheEnd(this List<string> list, string value)
        {
            for (int i = 0; i < list.Count; i++)
            {
                list[i] = value + list[i];
            }
        }
        public static void AddValueToAllAtTheStartAndEnd(this List<string> list, string startValue, string endValue)
        {
            for (int i = 0; i < list.Count; i++)
            {
                list[i] = startValue + list[i] + endValue;
            }
        }

        //public static void AddValueToAllAtTheStartAndEnd(this List<ImageInfo> list, string startValue, string bigEndValue, string smallEndValeu)
        //{
        //    for (int i = 0; i < list.Count; i++)
        //    {
        //        list[i].ImageSrcBig = startValue + list[i].ImageSrcBig + bigEndValue;
        //        list[i].ImageSrcSmall = startValue + list[i].ImageSrcSmall + smallEndValeu;
        //    }
        //}

        /// <summary>
        /// Adds a list of strings separated by ", ". This addition occurs depending of the condition passed to the Func predicate
        /// </summary>
        /// <param name="list">list that contains the string</param>
        /// <returns>returns a string containing the values of the list separated by ", " or an empty string if the list is empty</returns>
        //public static string ConcatStringsFromList(this List<ImageInfo> list, Func<ImageInfo, bool> predicate)
        //{
        //    string result = String.Empty;
        //    for (int i = 0; i < list.Count; i++)
        //    {
        //        if (predicate(list[i]))
        //        {
        //            result += list[i].ImageSrcBig;
        //            if ((i + 1) < list.Count)
        //            {
        //                result += ", ";
        //            }
        //        }
        //    }

        //    return result;
        //}


        //public static void CompleteImageInfoList(this List<ImageInfo> list, string adNumber, Guid id, string name)
        //{
        //    List<ImageInfo> items = list.Where(item => item.ImageSrcBig == adNumber && (item.Id == Guid.Empty || item.Id == null) ).ToList();

        //    for (int i = 0; i < items.Count; i++)
        //    {
        //        items[i].Id = id;
        //    }

        //}

        /// <summary>
        /// Checks if a List is not null and if the count is greater than 0
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns>returns true if all conditions match or false otherwise</returns>
        public static bool me_IsValid<T>(this List<T> list) 
        {
            if (list != null && list.Count > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Checks if a List is not null and if the count is greater than 0
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns>returns true if all conditions match or false otherwise</returns>
        public static bool me_IsValid<T>(this IEnumerable<T> list)
        {
            if (list != null && list.Count() > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Checks if a List is not null and if the count is greater than 0
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns>returns true if all conditions match or false otherwise</returns>
        public static bool me_IsValid<T>(this ICollection<T> list)
        {
            if (list != null && list.Count > 0)
            {
                return true;
            }
            return false;
        }

        public static void me_IsValidThrowExceptions<T>(this List<T> list)
        {
            if (list == null) throw new NullListException();
            if (list.Count() <= 0) throw new EmptyListException();
        }


        /// <summary>
        /// Get the list if it's valid or an empty list if not valid 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static List<T> me_GetValue<T>(this List<T> list)
        {
            if (list.me_IsValid()) 
            {
                return list;
            }
            return new List<T>();
        }

        /// <summary>
        /// Joins a List using a separater(can be empty);
        /// </summary>
        /// <param name="array"></param>
        /// <param name="separation">string that will be used to join all the items in the array</param>
        /// <param name="textFront">text that can be inserted before each item</param>
        /// <param name="textLast">text that can be inserted before each item</param>
        /// <returns>returns a string that represents the specified array</returns>
        public static string me_JoinList(this List<string> array, string separation, string textFront, string textLast)
        {
            string result = String.Empty;
            if (array.me_IsValid())
            {
                string tempSeparater = separation.me_GetValue();
                string tempTextFront = textFront.me_GetValue();
                string tempTextLast = textLast.me_GetValue();

                for (int i = 0; i < array.Count; i++)
                {
                    result += ((i > 0) ? tempSeparater : "") + tempTextFront + array[i].me_GetValue() + tempTextLast;
                }
            }

            return result;
        }
    }
}
